//
// Created by ClouD on 23.3.29.
//

#include "adc.h"
#include "spi.h"
#include "tim.h"

#include "led.h"
#include "drv8311p.h"
#include "mt6816ct-std.h"

/*-------------------LED-------------------*/

void LED_Init() {
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);      // TIM2_CH2(pwm)(LED_R)
  HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);      // TIM16_CH1(pwm)(LED_G)
  HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);      // TIM17_CH1(pwm)(LED_B)
}


void LED_Set_RGB(uint8_t R, uint8_t G, uint8_t B) {
  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, (uint16_t) (__HAL_TIM_GET_AUTORELOAD(&htim2) * R / 255.0));
  __HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, (uint16_t) (__HAL_TIM_GET_AUTORELOAD(&htim16) * G / 255.0));
  __HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1, (uint16_t) (__HAL_TIM_GET_AUTORELOAD(&htim17) * B / 255.0));
}


void LED_Get_RGB_t(RGB_t* RGB) {
  RGB->R = 255.0 * (float )__HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2) / (float )__HAL_TIM_GET_AUTORELOAD(&htim2);
  RGB->G = 255.0 * (float )__HAL_TIM_GET_COMPARE(&htim16, TIM_CHANNEL_1) / (float )__HAL_TIM_GET_AUTORELOAD(&htim16);
  RGB->B = 255.0 * (float )__HAL_TIM_GET_COMPARE(&htim17, TIM_CHANNEL_1) / (float )__HAL_TIM_GET_AUTORELOAD(&htim17);
}

/*-------------------DRV8311P-------------------*/

void DRV_PWM_Init(){
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);      // TIM3_CH4(pwm)(DRV_8311_PWM_SYN)
  HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_4);
}


void DRV_ADC_DMA_Init(uint16_t* adc_dma_buff, uint8_t dma_buff_size){
  if(HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_dma_buff, dma_buff_size) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
}

void DRV_ADC_Start_DMA(uint16_t *adc_dma_buff, uint8_t dma_buff_size){
  if(HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_dma_buff, dma_buff_size) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
}

void DRV_ADC_Stop_DMA(){
  if(HAL_ADC_Stop_DMA(&hadc1) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
}

void DRV_SPI_Init(){
  HAL_Delay(1);
  volatile uint16_t init_reg;
  init_reg = DRV_Get_REG(PWMG_CTRL, DRV_DEVICE_ID_0);
  (*(PWMG_CTRL_t*)&init_reg).PWM_OSC_SYNC=2;
  (*(PWMG_CTRL_t*)&init_reg).PWMCNTR_MODE=0;//Up/Down Mode
  (*(PWMG_CTRL_t*)&init_reg).PWM_EN=1;      //PWM_GEN enabled
  DRV_Set_PWMG_CTRL((PWMG_CTRL_t*)&init_reg, DRV_DEVICE_ID_0);

  init_reg=DRV_Get_REG(DRVF_CTRL, DRV_DEVICE_ID_0);
  (*(DRVF_CTRL_t*)&init_reg).OCP_LVL=1; //OCP 5A
  DRV_Set_DRVF_CTRL((DRVF_CTRL_t*)&init_reg, DRV_DEVICE_ID_0);

  init_reg=DRV_Get_REG(DRV_CTRL, DRV_DEVICE_ID_0);
  (*(DRV_CTRL_t*)&init_reg).TDEAD_CTRL=1; //Deadtime 200ns
  (*(DRV_CTRL_t*)&init_reg).SLEW_RATE=3; //Slew rate is 230 V/μs
  DRV_Set_DRV_CTRL((DRV_CTRL_t*)&init_reg, DRV_DEVICE_ID_0);

  init_reg=DRV_Get_REG(CSA_CTRL, DRV_DEVICE_ID_0);
  (*(CSA_CTRL_t*)&init_reg).CSA_GAIN=2; //CSA gain is 1 V/A
  DRV_Set_CSA_CTRL((CSA_CTRL_t*)&init_reg, DRV_DEVICE_ID_0);

  DRV_FLT_Clear(DRV_DEVICE_ID_0);

  DRV_Set_PWMG_Period(DRV_PWM_PRD_T - 1, DRV_DEVICE_ID_0);
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc){
  if(hadc->Instance==ADC1){
    DRV_ADC_Stop();
    DRV_Mean_Cal();
    DRV_ADC_Start();
  }
}

void DRV_tSPI_TransmitReceive(uint16_t* drv_write_buff,uint16_t* drv_read_buff,uint8_t num){
  HAL_GPIO_WritePin(nSCS_GPIO_Port,nSCS_Pin,GPIO_PIN_SET);
  HAL_GPIO_WritePin(nSCS_GPIO_Port,nSCS_Pin,GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive_DMA(&hspi1,(uint8_t *)drv_write_buff,(uint8_t *)drv_read_buff,num);
}

/*-------------------MT6816CT-STD-------------------*/

void MT6816_SPI_TransmitReceive(uint8_t* pTxData, uint8_t* pRxData,uint8_t size){
  HAL_GPIO_WritePin(CSN_GPIO_Port,CSN_Pin,GPIO_PIN_SET);
  HAL_GPIO_WritePin(CSN_GPIO_Port,CSN_Pin,GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive_DMA(&hspi2, pTxData, (uint8_t *) pRxData, size);
}